/*
 * Modified sample code from mgwt showcase
 * Copyright 2010 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.dhbw.vs.permachat.client.activities.chatroomlist;

import java.util.List;

import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.dom.client.event.tap.HasTapHandlers;
import com.googlecode.mgwt.ui.client.dialog.PopinDialog;
import com.googlecode.mgwt.ui.client.widget.HeaderButton;
import com.googlecode.mgwt.ui.client.widget.HeaderPanel;
import com.googlecode.mgwt.ui.client.widget.LayoutPanel;
import com.googlecode.mgwt.ui.client.widget.ProgressIndicator;
import com.googlecode.mgwt.ui.client.widget.ScrollPanel;
import com.googlecode.mgwt.ui.client.widget.buttonbar.ButtonBar;
import com.googlecode.mgwt.ui.client.widget.buttonbar.ButtonBarButtonBase;
import com.googlecode.mgwt.ui.client.widget.buttonbar.ButtonBarSpacer;
import com.googlecode.mgwt.ui.client.widget.buttonbar.PreviousSlideButton;
import com.googlecode.mgwt.ui.client.widget.buttonbar.RefreshButton;
import com.googlecode.mgwt.ui.client.widget.celllist.BasicCell;
import com.googlecode.mgwt.ui.client.widget.celllist.CellListWithHeader;
import com.googlecode.mgwt.ui.client.widget.celllist.HasCellSelectedHandler;

import de.dhbw.vs.permachat.client.dto.ChatRoom;

/**
 * @author flo
 * 
 */
public class ChatRoomListViewGwtImpl implements ChatRoomListView {

	private CellListWithHeader<ChatRoom> list;
	private LayoutPanel main;
	private HeaderPanel headerPanel;
	private HeaderButton headerBackButton;
	private PopinDialog progressDialog;
	private ButtonBarButtonBase refreshButton;
	private ScrollPanel scrollPanel;
	private ButtonBarButtonBase logoutButton;

	/**
	 * 
	 */
	public ChatRoomListViewGwtImpl() {
		main = new LayoutPanel();

		headerPanel = new HeaderPanel();

		headerBackButton = new HeaderButton();

		headerPanel.setLeftWidget(headerBackButton);
		headerBackButton.setBackButton(true);
		//headerBackButton.setVisible(!MGWT.getOsDetection().isAndroid());
		headerBackButton.setVisible(false);

		main.add(headerPanel);

		scrollPanel = new ScrollPanel();

		list = new CellListWithHeader<ChatRoom>(new BasicCell<ChatRoom>() {

			@Override
			public String getDisplayString(ChatRoom model) {
				return model.getName();
			}

			@Override
			public boolean canBeSelected(ChatRoom model) {
				return true;
			}
		});

		list.getCellList().setRound(true);
		list.getCellList().addStyleName("emphFirstLi");

		scrollPanel.setWidget(list);
		scrollPanel.setScrollingEnabledX(false);

		main.add(scrollPanel);
		
		
		ButtonBar footerPanel = new ButtonBar();
		logoutButton = new PreviousSlideButton();
		refreshButton = new RefreshButton();
		footerPanel.add(logoutButton);
		footerPanel.add(new ButtonBarSpacer());
		footerPanel.add(refreshButton);
		
		main.add(footerPanel);

	}

	@Override
	public Widget asWidget() {
		return main;
	}

	@Override
	public void setTitle(String text) {
		headerPanel.setCenter(text);

	}

	@Override
	public HasTapHandlers getBackButton() {
		return headerBackButton;
	}

	@Override
	public HasCellSelectedHandler getCellSelectedHandler() {
		return list.getCellList();
	}

	@Override
	public void setLeftButtonText(String text) {
		headerBackButton.setText(text);

	}

	@Override
	public void setChatRoomList(List<ChatRoom> chatRoomList) {
		list.getCellList().render(chatRoomList);
		this.scrollPanel.refresh();
		

	}

	@Override
	public HasText getFirstHeader() {
		return list.getHeader();
	}

	@Override
	public void showProgressIndicator() {
		this.progressDialog = new PopinDialog();
		this.progressDialog.add(new ProgressIndicator());
		this.progressDialog.setCenterContent(true);
		this.progressDialog.setHideOnBackgroundClick(false);
		this.progressDialog.setPanelToOverlay(this.main);
		this.progressDialog.setShadow(true);
		
		this.progressDialog.show();
	}

	@Override
	public void hideProgressIndicator() {
		if(this.progressDialog != null){
			this.progressDialog.hide();
			this.progressDialog = null;
		}
	}

	@Override
	public void emphasizeFirstRoom(boolean doEmph) {
		if(doEmph){
			list.getCellList().addStyleName("emphFirstLi");
		} else{
			list.getCellList().removeStyleName("emphFirstLi");
		}
	}

	@Override
	public HasTapHandlers getRefreshButton() {
		return this.refreshButton;
	}

	@Override
	public HasTapHandlers getLogoutButton() {
		return this.logoutButton;
	}

}
